﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using AEM.Common;
using AEM.Data;
using AEM.Utils;

namespace AEM.Controls
{
    /// <summary>
    /// StatusRatio.xaml 的交互逻辑
    /// </summary>
    public partial class StatusRatio : UserControl
    {
        public StatusRatio()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(StatusRatio_Loaded);
            this.btn_select.Click += new RoutedEventHandler(btn_select_Click);
            this.datefrom.ValueChanged += new EventHandler(datefrom_SelectedDateChanged);
            this.dateto.ValueChanged += new EventHandler(dateto_SelectedDateChanged);
            this.listStatusRatio.SelectionChanged += new SelectionChangedEventHandler(listStatusRatio_SelectionChanged);
            this.btn_reset.Click += new RoutedEventHandler(btn_reset_Click);
        }

        void btn_reset_Click(object sender, RoutedEventArgs e)
        {
            this.liftNum.SelectedItem = null;
            this.datefrom.Value = DateTime.Now;
            this.dateto.Value = DateTime.Now;
        }

        void StatusRatio_Loaded(object sender, RoutedEventArgs e)
        {
            if (null != Config.LiftInftList)
                this.liftNum.ItemsSource = Config.LiftInftList;
            this.liftNum.DisplayMemberPath = "LiftSite";
            this.liftNum.Items.Refresh();
            //this.liftNum.SelectedIndex = 0;

            DateTime s = DataLogHelper.GetEarliestTime();
            if (null != s)
            {
                this.datefrom.DisplayDateStart = s;
                this.dateto.DisplayDateStart = s;
            }
            else
            {
                this.datefrom.DisplayDateStart = DateTime.Now;
            }
            this.datefrom.DisplayDateEnd = DateTime.Now;
            this.dateto.DisplayDateEnd = DateTime.Now;
        }

        void dateto_SelectedDateChanged(object sender, EventArgs e)
        {
            this.datefrom.DisplayDateEnd = this.dateto.Value;
        }

        void datefrom_SelectedDateChanged(object sender, EventArgs e)
        {
            this.dateto.DisplayDateStart = this.datefrom.Value;
        }

        void btn_select_Click(object sender, RoutedEventArgs e)
        {
            this.btn_reset.IsEnabled = false;
            this.msg.Text = "";
            if (null == this.datefrom.Value)
            {
                this.msg.Text = string.Format(this.TryFindResource("AskSelectOne") as string, this.TryFindResource("DateFrom") as string);
                this.btn_reset.IsEnabled = true; return;
            }
            if (null == this.dateto.Value)
            {
                this.msg.Text = string.Format(this.TryFindResource("AskSelectOne") as string, this.TryFindResource("DateTo") as string);
                this.btn_reset.IsEnabled = true; return;
            }
            DateTime s = this.datefrom.Value.Value;
            DateTime t = this.dateto.Value.Value;

            this.msg.Text = this.TryFindResource("Searching") as string;
            if (null != this.liftNum.SelectedItem)
            {
                StatusRatioItem sri = CreateItem(this.liftNum.SelectedItem as Data.Model.LiftInfo, s, t);
                this.listStatusRatio.ItemsSource = new List<StatusRatioItem>() { sri };
            }
            else
            {
                List<StatusRatioItem> list = new List<StatusRatioItem>();
                foreach (Data.Model.LiftInfo r in Config.LiftInftList)
                {
                    list.Add(CreateItem(r, s, t));
                }
                this.listStatusRatio.ItemsSource = list;
            }

            this.listStatusRatio.SelectedIndex = 0;
            this.btn_reset.IsEnabled = true;
            this.msg.Text = this.TryFindResource("SearchOK") as string;
        }

        private StatusRatioItem CreateItem(Data.Model.LiftInfo l, DateTime s, DateTime t)
        {
            try
            {
                StatusRatioItem i = new StatusRatioItem();
                i.LiftInfo = l;
                i.ShortName = l.ShortName;

                double os, oh, nc, nl;
                double x = DataLogHelper.GetStatusCount(l, s, t, out os, out oh, out nc, out nl);
                i.OutOfService = (os / 1000).toInt64();
                i.ShowOS = (new TimeSpan(0, 0, 0, i.OutOfService.toInt32())).ToString("g");
                i.Overhaul = (oh / 1000).toInt64();
                i.ShowOH = (new TimeSpan(0, 0, 0, i.Overhaul.toInt32())).ToString("g");
                i.NC = (nc / 1000).toInt64();
                i.ShowNC = (new TimeSpan(0, 0, 0, i.NC.toInt32())).ToString("g");
                i.Normal = (nl / 1000).toInt64();
                i.ShowNL = (new TimeSpan(0, 0, 0, i.Normal.toInt32())).ToString("g");
                return i;
            }
            catch
            {
                return null;
            }
        }

        void listStatusRatio_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RefreshSeries(this.listStatusRatio.SelectedItem as StatusRatioItem);
        }

        void RefreshSeries(StatusRatioItem sri)
        {
            if (null == sri) return;
            List<KeyValuePair<string, double>> valueList = new List<KeyValuePair<string, double>>();
            double sum = sri.Normal + sri.OutOfService + sri.Overhaul + sri.NC;
            if (sum == 0) sum = 1;
            double per = 0;
            per = sri.Normal / sum;
            valueList.Add(new KeyValuePair<string, double>(this.TryFindResource("Normal") as string + per.ToString("(#0.## %)"), sri.Normal));
            per = sri.OutOfService / sum;
            valueList.Add(new KeyValuePair<string, double>(this.TryFindResource("OutOfService") as string + per.ToString("(#0.## %)"), sri.OutOfService));
            per = sri.Overhaul / sum;
            valueList.Add(new KeyValuePair<string, double>(this.TryFindResource("Overhaul") as string + per.ToString("(#0.## %)"), sri.Overhaul));
            per = sri.NC / sum;
            valueList.Add(new KeyValuePair<string, double>(this.TryFindResource("NC") as string + per.ToString("(#0.## %)"), sri.NC));
            this.pieStatusSeries.DataContext = valueList;

            this.pieStatusRatio.LegendTitle = sri.LiftInfo.LiftSite;// +"/" + this.TryFindResource("TimeUnit") as string;
        }
    }

    public class StatusRatioItem
    {
        public string ShortName { get; set; }
        public Data.Model.LiftInfo LiftInfo { get; set; }
        public double Normal { get; set; }
        public string ShowNL { get; set; }
        public double OutOfService { get; set; }
        public string ShowOS { get; set; }
        public double Overhaul { get; set; }
        public string ShowOH { get; set; }
        public double NC { get; set; }
        public string ShowNC { get; set; }
    }
}
